const app = getApp()
const util = require("../../utils/utils.js")

// pages/forum-home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    posts: [],
    headimgs: [],
    nicknames: [],
    likes: [],
    isCommentPopupShow: false,
    commentTextareaValue: "",
    targetIndex: "",
    loginUserInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: "贴吧"
    });
    this.setData({
      loginUserInfo: app.globalData.userInfo
    })
    var that = this
    wx.request({
      url: 'http://localhost:8888/posting/getAllPosting',
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          posts: res.data.data
        })
        for (var i = 0; i < that.data.posts.length; i++) {
          var iPhoto = "posts[" + i + "].photo"
          var iTag = "posts[" + i + "].tag"
          that.setData({
            [iPhoto]: util.strToArr(that.data.posts[i].photo),
            [iTag]: util.strToArr(that.data.posts[i].tag)
          })
        }
        that.doILikeIt()
      }
    })
    wx.request({
      url: 'http://localhost:8888/posting/getPostingUserHeadImg',
      method: 'GET',
      data: {
        type: 0,
        user: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          headimgs: res.data.data
        })
      }
    })
    wx.request({
      url: 'http://localhost:8888/posting/getPostingUserName',
      method: 'GET',
      data: {
        type: 0,
        user: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          nicknames: res.data.data
        })
      }
    });
    //console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onPostTapped: function (event) {
    app.globalData.postingId = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../forum-detail/detail'
    });
  },
  preview(event) {
    wx.previewImage({
      current: this.data.posts[event.currentTarget.dataset.iindex].photo[event.currentTarget.dataset.index],
      urls: this.data.posts[event.currentTarget.dataset.iindex].photo,
    })
  },
  doILikeIt: function () {
    var loginAccount = app.globalData.loginAccount
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/doILikeThem',
      method: 'GET',
      data: {
        type: 0,
        username: loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          likes: res.data.data
        })
      }
    })
  },
  doLike: function (event) {
    //console.log("do like");
    var loginAccount = app.globalData.loginAccount
    var iindex = event.currentTarget.dataset.iindex
    var postingId = this.data.posts[iindex].id
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/doLike',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var iLike = "likes[" + iindex + "]"
        var iPost = "posts[" + iindex + "].likes"
        var likes = that.data.posts[iindex].likes + 1
        that.setData({
          [iLike]: 1,
          [iPost]: likes
        })
      }
    })
  },
  dontLike: function (event) {
    //console.log("dont like");
    var loginAccount = app.globalData.loginAccount
    var iindex = event.currentTarget.dataset.iindex
    var postingId = this.data.posts[iindex].id
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/dontLike',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var iLike = "likes[" + iindex + "]"
        var iPost = "posts[" + iindex + "].likes"
        var likes = that.data.posts[iindex].likes - 1
        that.setData({
          [iLike]: 0,
          [iPost]: likes
        })
      }
    })
  },
  showCommentPopup: function (event) {
    let index = event.currentTarget.dataset.iindex
    console.log(index);
    this.setData({
      isCommentPopupShow: true,
      targetIndex: index
    })
    // console.log(app.globalData.loginAccount);
  },

  hideCommentPopup: function () {
    this.setData({
      isCommentPopupShow: false
    })
  },
  updateCommentInputData: function (event) {
    this.setData({
      commentTextareaValue: event.detail.value
    });
  },
  sendComment: function (event) {
    //console.log("do like");
    let loginAccount = app.globalData.loginAccount
    let index = this.data.targetIndex
    let postingId = this.data.posts[index].id
    let content = this.data.commentTextareaValue
    var that = this
    if (content.match(/^[ ]*$/)) {
      wx.showToast({
        title: '评论不能为空',
        icon: 'error',
        duration: 1500
      });
      return;
    }
    wx.request({
      url: 'http://localhost:8888/comments/insertComments',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId,
        content: content
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var commentVar = "posts[" + index + "].comments";
        var commentNum = that.data.posts[index].comments + 1;
        that.setData({
          [commentVar]: commentNum
        });
        that.hideCommentPopup();
        that.setData({
          commentTextareaValue: ""
        });
        wx.showToast({
          title: '评论发送成功!',
          icon: 'success',
          duration: 2000
        });
      }
    })
  },
  showLoginComment() {
    wx.showToast({
      title: '登录后方可评论',
      icon: "error"
    })
  },
  showLoginLike() {
    wx.showToast({
      title: '登录后方可点赞',
      icon: "error"
    })
  },

})